<?php
class Ecore_Paging
{
	var $page 	= 0;
	var $limit 	= 0;
	var $total 	= 0;
	var $url 	= '';
	var $name	= '';

	public function __construct($intTotal, $intLimit, $intOffset, $strUrl = '', $strPage='page')
	{
		$this->total 	= ($intTotal);
		$this->page  	= ($intOffset / $intLimit) + 1;
		$this->limit	= ($intLimit);
		$this->offset 	= ($intOffset);
		$this->url	 	= ($strUrl);
		$this->name		= ($strPage);
	}

	public function get($intDisplayPage = 10)
	{
		$strNav = '';
		$intPageTotal 	= ceil($this->total / $this->limit);
		$intDelta		= ceil($intDisplayPage / 2);
		if ($intPageTotal > $intDisplayPage) {
			if ($this->page <= $intDelta) {
				$intStartLoop 	= 1;
				$intEndLoop		= $intDisplayPage;
			} elseif ($this->page >= $intPageTotal - $intDelta) {
				$intStartLoop 	= $intPageTotal - $intDisplayPage + 1;
				$intEndLoop		= $intPageTotal;
			} else {
				$intStartLoop 	= $this->page - $intDelta + 1;
				$intEndLoop		= $this->page + $intDelta;
			}
		} else {
			$intStartLoop = 1;
			$intEndLoop = $intPageTotal;
		}
		$html = '';
		//echo $this->page; exit();
		if ($intEndLoop > 1) {
			for ($i = $intStartLoop; $i <= $intEndLoop; $i++) {
				if ($i == $this->page) {
					$strNav .= "<span>$i</span>";
				} else {
					$strNav .= " <a href=\"" . $this->url . $this->name . "=$i\">$i</a> ";
				}
			}
			if ($this->page > 1) {
				$strFirst = " <a href=\"" . $this->url . $this->name . "=1\">&laquo;</a>";
				$strPrev  = " <a href=\"" . $this->url . $this->name . "=" . ($this->page - 1) . "\">&lsaquo;</a> ";
			} else {
				$strFirst = '';
				$strPrev  = '';
			}
			if ($this->page < $intPageTotal) {				
				$strLast = " <a href=\"" . $this->url . $this->name . "=" . $intPageTotal . "\">&raquo;</a> ";
				$strNext = " <a href=\"" . $this->url . $this->name . "=" . ($this->page + 1) . "\">&rsaquo;</a> ";
			} else {
				$strLast = '';
				$strNext = '';
			}
			$html = "<div class=\"paging\">$strFirst $strPrev $strNav $strNext $strLast</div>";
		}
		return $html;
	}

	public function getAjax($intDisplayPage = 10, $strContainer = '')
	{
		$strNav = '';
		$intPageTotal 	= ceil($this->total / $this->limit);
		$intDelta		= ceil($intDisplayPage / 2);
		if ($intPageTotal > $intDisplayPage) {
			if ($this->page <= $intDelta) {
				$intStartLoop 	= 1;
				$intEndLoop		= $intDisplayPage;
			} elseif ($this->page >= $intPageTotal - $intDelta) {
				$intStartLoop 	= $intPageTotal - $intDisplayPage + 1;
				$intEndLoop		= $intPageTotal;
			} else {
				$intStartLoop 	= $this->page - $intDelta + 1;
				$intEndLoop		= $this->page + $intDelta;
			}
		} else {
			$intStartLoop = 1;
			$intEndLoop = $intPageTotal;
		}
		$html = '';
		if ($intEndLoop > 1) {		
			for ($i = $intStartLoop; $i <= $intEndLoop; $i++) {
				if ($i == $this->page) {
					$strNav .= "<span>$i</span>";
				} else {
					$strNav .= " <a href=\"javascript:;\" onclick=\"javascript:paging('" . $this->url . $this->name . "=$i','$strContainer',".$i.")\">$i</a> ";
				}
			}
			if ($this->page > 1) {
				$strFirst = " <a href=\"javascript:;\" onclick=\"javascript:paging('" . $this->url . $this->name . "=1','$strContainer',1)\">&laquo;</a> ";
				$strPrev  = " <a href=\"javascript:;\" onclick=\"javascript:paging('" . $this->url . $this->name . "=" . ($this->page - 1) . "','$strContainer',".($this->page - 1).")\">&lsaquo;</a> ";
			} else {
				$strFirst = '';
				$strPrev  = '';
			}
			if ($this->page < $intPageTotal) {
				$strLast = " <a href=\"javascript:;\" onclick=\"javascript:paging('" . $this->url . $this->name . "=" . $intPageTotal . "','$strContainer',".$intPageTotal.")\">&raquo;</a> ";
				$strNext = " <a href=\"javascript:;\" onclick=\"javascript:paging('" . $this->url . $this->name . "=" . ($this->page + 1) . "','$strContainer',".($this->page + 1).")\">&rsaquo;</a> ";
			} else {
				$strLast = '';
				$strNext = '';
			}
			$html = "<div class=\"paging\">$strFirst $strPrev $strNav $strNext $strLast</div>";
		}
		return $html;
	}
	
	public function getAjaxJson($intDisplayPage = 10, $strContainer = '')
	{
		$strNav = '';
		$intPageTotal 	= ceil($this->total / $this->limit);
		$intDelta		= ceil($intDisplayPage / 2);
		if ($intPageTotal > $intDisplayPage) {
			if ($this->page <= $intDelta) {
				$intStartLoop 	= 1;
				$intEndLoop		= $intDisplayPage;
			} elseif ($this->page >= $intPageTotal - $intDelta) {
				$intStartLoop 	= $intPageTotal - $intDisplayPage + 1;
				$intEndLoop		= $intPageTotal;
			} else {
				$intStartLoop 	= $this->page - $intDelta + 1;
				$intEndLoop		= $this->page + $intDelta;
			}
		} else {
			$intStartLoop = 1;
			$intEndLoop = $intPageTotal;
		}
		$html = '';
		if ($intEndLoop > 1) {		
			for ($i = $intStartLoop; $i <= $intEndLoop; $i++) {
				if ($i == $this->page) {
					$strNav .= "<span>$i</span>";
				} else {
					$strNav .= " <a href=\"javascript:;\" onclick=\"javascript:paging2('" . $this->url . $this->name . "=$i','$strContainer')\">$i</a> ";
				}
			}
			if ($this->page > 1) {
				$strFirst = " <a href=\"javascript:;\" onclick=\"javascript:paging2('" . $this->url . $this->name . "=1','$strContainer')\">&laquo;</a> ";
				$strPrev  = " <a href=\"javascript:;\" onclick=\"javascript:paging2('" . $this->url . $this->name . "=" . ($this->page - 1) . "','$strContainer')\">&lsaquo;</a> ";
			} else {
				$strFirst = '';
				$strPrev  = '';
			}
			if ($this->page < $intPageTotal) {
				$strLast = " <a href=\"javascript:;\" onclick=\"javascript:paging2('" . $this->url . $this->name . "=" . $intPageTotal . "','$strContainer')\">&raquo;</a> ";
				$strNext = " <a href=\"javascript:;\" onclick=\"javascript:paging2('" . $this->url . $this->name . "=" . ($this->page + 1) . "','$strContainer')\">&rsaquo;</a> ";
			} else {
				$strLast = '';
				$strNext = '';
			}
			$html = "<div class=\"paging\">$strFirst $strPrev $strNav $strNext $strLast</div>";
		}
		return $html;
	}
}
?>